package com.yuelan.codelib.utils.encryption;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

import com.yuelan.codelib.utils.LOG;

public class MD5Encoder {
	public static long sysstatisCrcCalcFile(File file) {
		long checksum = 0;
		try {
			CheckedInputStream cis = null;
			cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
			byte[] buf = new byte[1024];
			while (cis.read(buf) >= 0)
				;
			checksum = cis.getChecksum().getValue();
			cis.close();
		} catch (IOException e) {
			e.printStackTrace();
			checksum = 0;
		}

		LOG.v("sysstatisCrcCalcFile checksum = " + checksum);
		return checksum;
	}

	public static String EncoderByMd5(String inStr) {
		try {
			MessageDigest bmd5 = MessageDigest.getInstance("MD5");
			bmd5.update(inStr.getBytes("utf-8"));
			int i;
			StringBuffer buf = new StringBuffer();
			byte[] b = bmd5.digest();
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			return buf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String EncoderByMd5_16(String inStr) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(inStr.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			// // LogUtil.v("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		// 16位加密，从第9位到25位
		return md5StrBuff.substring(8, 24).toString().toUpperCase();
	}

	public static String EncoderByMd5_End16(String inStr) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(inStr.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			// LogUtil.v("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		// 16位加密，从第9位到25位
		return md5StrBuff.substring(16).toString().toUpperCase();
	}
}
